using System;
using System.Collections.Generic;
using System.Text;

namespace NBox.Service.EmailSender
{
    public sealed class EmailSenderManager
    {
        private static IEmailSender _sender = null;
        private static IConfigurationReader _configurationReader = new ConfigurationReader();
        private static object _loadLock = new object();
        private static readonly string COMMON_SECTION_EMAILSENDER = "nbox.service/emailsender";

        private EmailSenderManager()
        {
        }

        private static IEmailSender GetSender()
        {
            ConfigurationSetting section = null;
            try
            {
                section = (ConfigurationSetting)ConfigurationReader.GetSection(COMMON_SECTION_EMAILSENDER);
            }
            catch (Exception exception)
            {
                throw new ConfigurationException("Could not configure NBox.Service.EmailSender from configuration section 'nbox.service/emailsender'.", exception);
            }

            if ((section != null) && !typeof(IEmailSender).IsAssignableFrom(section.SenderType))
            {
                throw new ConfigurationException("Specified Sender does not implement IEmailSender.  Check implementation of class " + section.SenderType.AssemblyQualifiedName);
            }

            IEmailSender sender = null;
            if (section != null)
            {
                try
                {
                    if (section.Properties.Count > 0)
                    {
                        object[] args = new object[] { section.Properties };
                        sender = (IEmailSender)Activator.CreateInstance(section.SenderType, args);
                    }
                    else
                    {
                        sender = (IEmailSender)Activator.CreateInstance(section.SenderType);
                    }
                }
                catch (Exception exception2)
                {
                    throw new ConfigurationException("Unable to create instance of type " + section.SenderType.FullName + ". Possible explanation is lack of zero arg and single arg NameValueCollection constructors", exception2);
                }
                return sender;
            }

            IEmailSender sender2 = new NBox.Service.EmailSender.MockEmailSender();
            System.Diagnostics.Trace.WriteLine("Unable to read configuration section nbox.service/emailsender.  Using no-op implemenation.");
            return sender2;
        }

        public static IEmailSender Sender
        {
            get
            {
                if (_sender == null)
                {
                    lock (_loadLock)
                    {
                        if (_sender == null)
                        {
                            _sender = GetSender();
                        }
                    }
                }
                return _sender;
            }
            set
            {
                lock (_loadLock)
                {
                    _sender = value;
                }
            }
        }

        public static IConfigurationReader ConfigurationReader
        {
            get
            {
                return _configurationReader;
            }
            set
            {
                _configurationReader = value;
            }
        }
    }
}
